/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ensa.googlemapsandroidv2;

import java.util.ArrayList;
import java.util.List;

import com.ensa.googlemapsandroidv2.R;
import com.ensa.model.Personne;

import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ScreenSlidePageFragment extends Fragment {

	public static final String ARG_PAGE = "page";

	List<String> nom = new ArrayList<String>();
	List<Integer> age = new ArrayList<Integer>();
	List<String> adresse = new ArrayList<String>();
	List<String> photo = new ArrayList<String>();
	List<String> numTel = new ArrayList<String>();
	static List<Personne> personne;
	private int mPageNumber;

	public static ScreenSlidePageFragment create(int pageNumber,
			List<Personne> personne2) {
		personne = personne2;
		ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, pageNumber);
		fragment.setArguments(args);
		return fragment;
	}

	public ScreenSlidePageFragment() {
		for (Personne p : personne) {
			nom.add(p.getNom());
			age.add(p.getAge());
			adresse.add(p.getAdresse());
			photo.add(p.getPhoto());
			numTel.add(p.getTel());
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPageNumber = getArguments().getInt(ARG_PAGE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout containing a title and body text.
		ViewGroup rootView = (ViewGroup) inflater.inflate(
				R.layout.fragment_screen_slide_page, container, false);

		// Set the title view to show the page number.
		((TextView) rootView.findViewById(android.R.id.text1))
				.setText(getString(R.string.title_template_step,
						mPageNumber + 1));
		((TextView) rootView.findViewById(R.id.nomVal)).setText(nom
				.get(mPageNumber));
		((TextView) rootView.findViewById(R.id.ageVal)).setText(age
				.get(mPageNumber) + " ans");
		((TextView) rootView.findViewById(R.id.adresseVal)).setText(adresse
				.get(mPageNumber));
		((TextView) rootView.findViewById(R.id.telVal)).setText(numTel
				.get(mPageNumber));
		
		int x=mPageNumber;
		((ImageView) rootView.findViewById(R.id.imageView1)).setImageResource(R.drawable.image+x);
		//setImageDrawable(R.drawable.a);

		final Button appelBtn = (Button) rootView.findViewById(R.id.btnappler);

		/******** Dynamically create view elements **********/
		appelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Uri telnumber;
				Intent call;
				try {
					Thread.sleep(4);
					telnumber = Uri.parse("tel:"+numTel.get(mPageNumber));
					call = new Intent(Intent.ACTION_CALL, telnumber);
					startActivity(call);

				} catch (ActivityNotFoundException activityException) {
					Log.e("Exception", "Call failed", activityException);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		
		final Button repertoirBtn = (Button) rootView.findViewById(R.id.btnmodifier);
		
		/******** Dynamically create view elements **********/
		
		repertoirBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				Intent i = new Intent(getActivity().getBaseContext(), Authentification.class);
				i.putExtra("nom",nom.get(mPageNumber));
				i.putExtra("age",age.get(mPageNumber).toString());
				i.putExtra("adresse",adresse.get(mPageNumber));
				i.putExtra("tel",numTel.get(mPageNumber));
				i.putExtra("position",String.valueOf(mPageNumber));
				startActivity(i);
			}
		});

		return rootView;
	}

	/**
	 * Returns the page number represented by this fragment object.
	 */
	public int getPageNumber() {
		return mPageNumber;
	}
}
